package cn.com.wxd.util.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import cn.com.wxd.entity.message.PromptInfo;
import cn.com.wxd.entity.user.AccountInfo;
import cn.com.wxd.entity.user.UsersInfo;
import cn.com.wxd.service.weather.Weatherbean;
import cn.com.wxd.common.CharUtil;
import cn.com.wxd.util.user.UserCoreUtil;

public class MessageUtil {
    /**
     * 随机获取一则提示json，
     * 如果有优先的提示则推送优先的提示，否则推送一挑问候语或提示
     *
     * @return 如果有优先的提示则推送优先的消息，否则推送一挑问候语或提示
     */
    public static PromptInfo getPrompt(HttpServletRequest request) {
        int rannum = CharUtil.getIntRanNumber(1, 4);
        PromptInfo promptInfo = new PromptInfo();
        //TODO 假设没有重要消息
        switch (rannum) {

            case 1:
                promptInfo.setContent(getHelloUser(request));
                break;
            case 2:
                promptInfo.setContent( "你知道吗？友情链接申请地址为:http://您的域名/website/frendlink.html");
               break;
            case 3:
                promptInfo.setContent("你知道吗？友情链接接远程提交口开放啦！使用http://域名/service/RemoteFrendLink.do?"
                        + "link=add(delete)&key=链接文本&url=链接网址");
                break;
            case 4:
                promptInfo.setContent( "你知道吗？网站的RSS聚合频道的地址是：http://域名/website/rss.html,快去看看吧!");
                break;

            default:
                promptInfo.setContent( "现在没有提示！");
                break;
        }

        return promptInfo;
    }

    /**
     * 获取用户的问候语
     *
     * @return
     */
    private static String getHelloUser(HttpServletRequest request) {
        AccountInfo accountInfo = UserCoreUtil.getLoginAccount(request);
        String helloword;
        Weatherbean weatherBean = WebUtil.getWeather(request);
        Date today = new Date();
        SimpleDateFormat f = new SimpleDateFormat("MM月dd日");
        String time = f.format(today);
        if (accountInfo != null) {
            UsersInfo user = accountInfo.getUsersInfo();
            helloword = user.getNickName() + "，"
                    + WebUtil.getHelloTime() + "好！今天是"
                    + time + " " + WebUtil.getWeekOfDate();
            if (weatherBean != null) {
                helloword += "  天气：" + weatherBean.getCityName()
                        + " " + weatherBean.getStateDetailed()
                        + " " + weatherBean.getTem2()
                        + "℃ ~ " + weatherBean.getTem1() + "℃";
            }
        } else {
            helloword = "今天是"
                    + time + " " + WebUtil.getWeekOfDate();
            if (weatherBean != null) {
                helloword += "  天气：" + weatherBean.getCityName()
                        + " " + weatherBean.getStateDetailed()
                        + " " + weatherBean.getTem2()
                        + "℃ ~ " + weatherBean.getTem1() + "℃";
            }
        }
        return helloword;
    }
}
